# coding: utf-8
from datetime import datetime
from django.db import models
from lib.common import BasicManager
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from project.settings import DEFAULT_PAGESIZE


class ModelManagerSlider(BasicManager):

    def get_slider_list(self, page, is_show=None, sort=None, platform=None):
        result = self.values().order_by("-created_at")
        if is_show:
            result = result.filter(is_show=is_show)
        if sort:
            result = result.order_by("-sort")
        if platform:
            result = result.filter(platform=platform)
        paginator = Paginator(result, DEFAULT_PAGESIZE)
        try:
            result_page = paginator.page(page)
        except PageNotAnInteger:
            result_page = paginator.page(1)
        except EmptyPage as e:
            return {'retlist': [], 'curpageidx': page,
                    'hasnext': False, 'hasprevious': False, 'retcode': 0}
        return {'retlist': list(result_page), 'curpageidx': page,
                'hasnext': result_page.has_next(), 'hasprevious': result_page.has_previous(),
                'total_page': paginator.num_pages, 'retcode': 0}

    def get_slider(self, slider_id, is_show):
        return self.get(is_show=is_show, id=slider_id)


class Slider(models.Model):
    sort = models.IntegerField("排序", default=0)
    # 1:APP 2:H5 3:PC  4:小程序
    platform = models.PositiveSmallIntegerField("平台", default=1)
    is_show = models.PositiveSmallIntegerField("是否显示", default=0)
    # 1:活动 2:课程 3:普通展示  4:小程序
    types = models.PositiveSmallIntegerField('类型', default=1)
    thumb = models.CharField("封面链接", max_length=200)
    # 对应types  活动跳转活动链接
    url = models.CharField("跳转参数", max_length=150, null=True)
    created_at = models.DateTimeField("添加时间", auto_now_add=True)
    updated_at = models.DateTimeField("修改时间", auto_now=True)

    objects = ModelManagerSlider()

    class Meta:
        verbose_name = '首页banner'
        verbose_name_plural = verbose_name
        db_table = "momself_slider"

    def __str__(self):
        return self.platform


mm_Slider = Slider.objects
